package com.share.common.enums;

import lombok.Getter;

@Getter
public enum ContentTypeEnum {
    //通用
    BIN(".*", "application/octet-stream", "二进制文件下载流"),

    //html类型
    TXT(".txt", "text/plain", "txt文件"),
    JSON(".json", "application/Json", "json文件"),
    XML(".xml", "text/xml", "xml文件"),
    ASP(".asp", "text/asp", "asp文件"),
    HTML(".html", "text/html", "html文件"),
    JSP(".jsp", "text/html", "jsp文件"),
    MHTML(".mhtml", "message/rfc822", "mhtml文件"),

    //图片
    BMP(".bmp", "application/x-bmp", "bmp文件"),
    GIF(".gif", "image/gif", "gif文件"),
    JPEG(".jpeg", "image/jpeg", "jpeg文件"),
    JPG(".jpg", "image/jpeg", "jpg文件"),
    PNG(".png", "image/png", "png文件"),


    //视频
    AVI(".avi", "video/avi", "avi文件"),
    MPG(".mpg", "video/mpg", "mpg文件"),


    //办公
    DOC(".doc", "application/msword", "doc文件"),
    XLS(".xls", "application/vnd.ms-excel", "xls文件"),
    PPT(".ppt", "application/vnd.ms-powerpoint", "ppt文件"),
    PDF(".pdf", "application/pdf", "pdf文件"),
    PPS(".pps", "application/vnd.ms-powerpoint", "pps文件");

    public String suffix;
    public String contentType;
    public String name;

    private ContentTypeEnum(final String suffix, final String contentType, final String name) {
        this.suffix = suffix;
        this.contentType = contentType;
        this.name = name;
    }

    public static ContentTypeEnum toEnum(final String fileName) {
        ContentTypeEnum res = null;
        if (fileName != null && fileName.length() >= 2) {
            for (final ContentTypeEnum tag : ContentTypeEnum.values()) {
                if (fileName.endsWith(tag.suffix)) {
                    res = tag;
                    break;
                }
            }
        }
        return res;
    }

    public static String getContentType(final String fileName) {
        ContentTypeEnum contentType = toEnum(fileName);
        return contentType == null ? null : contentType.getContentType();
    }
}
